using ChatConsoleClient;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Configuration;
public delegate void ReceviceCaptchaHandeler(string _strCaptcha);
public class TcpClientSample
{
    public NetworkStream ns;

    public event ReceviceCaptchaHandeler OnReceiveCaptcha = null;

    public void Connect()
   {
        isRun = true;
        byte[] data = new byte[128000];
      string input;
      //int port;
 
      
      System.Console.WriteLine ("Please Enter the port number of Server:\n");
      //port = Int32.Parse( System.Console.ReadLine());
      try
      {
          string strServerAddress = ConfigurationSettings.AppSettings["ServerAddress"] == null ? "127.0.0.1" : ConfigurationSettings.AppSettings["ServerAddress"];
          string strServerPort = ConfigurationSettings.AppSettings["ServerPort"] == null ? "12345" : ConfigurationSettings.AppSettings["ServerPort"];
          server = new TcpClient(strServerAddress, Int32.Parse(strServerPort));
      } catch (SocketException)
      {
         Console.WriteLine("Unable to connect to server");
         return;
      }
      Console.WriteLine("Connected to the Server...");
      Console.WriteLine("Enter the message to send it to the Sever");
       ns = server.GetStream();

      StateObject state = new StateObject();
      state.workSocket = server.Client;
      server.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                  new AsyncCallback((this).OnReceive), state);
      Console.WriteLine("Disconnecting from server...");
   }


    public void OnReceive(IAsyncResult ar)
    {
        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;
        int bytesRead;

        if (handler.Connected)
        {

            // Read data from the client socket. 
            try
            {
                bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {
                    // There  might be more data, so store the data received so far.
                    state.sb.Remove(0, state.sb.Length);
                    state.sb.Append(Encoding.Unicode.GetString(
                                     state.buffer, 0, bytesRead));

                    // Display Text in Rich Text Box
                    content = state.sb.ToString();
                    //Console.WriteLine(content);
                    OnReceiveCaptcha(content);

                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(OnReceive), state);

                }
            }

            catch (SocketException socketException)
            {
                //WSAECONNRESET, the other side closed impolitely
                if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
                {
                    handler.Close();
                }
            }

        // Eat up exception....Hmmmm I'm loving eat!!!
            catch (Exception exception)
            {
                //MessageBox.Show(exception.Message + "\n" + exception.StackTrace);
            }
        }
    }



    public bool isRun = false;
    public TcpClient server;
}
